import type { App, Directive } from 'vue';
import dayjs from 'dayjs';

const formatTimeDirective: Directive = {
  mounted(el: Element) {
    const textContent = el.textContent as string;
    let timestamp = parseInt(textContent);
    if (textContent.length === 10) {
      timestamp = timestamp * 1000;
    }
    el.textContent = dayjs(timestamp).format('YYYY-MM-DD hh:mm:ss') as unknown as string;

    console.log('textContent', textContent);
  },
};

export function setupFormatTimeDirective(app: App) {
  app.directive('format-time', formatTimeDirective);
}
